Page({
  data: {
    randomImages: {
      game2048: '',
      snake: '',
      memory: '',
      tetris: '',
      sudoku: '',
      shooter: '',
      lianliankan: '',
      riddles: ''
    },
    gameList: [
      {
        id: 1,
        name: '2048小游戏',
        icon: '/images/games/2048.png',
        description: '经典的2048数字游戏，通过方向键滑动合并相同数字，目标是得到2048数字方块。',
        bgColor: '#faf8ef',
        playCount: 9872,
        isHot: true,
        path: '/pages/games/2048/2048'
      },
      {
        id: 2,
        name: '贪吃蛇',
        icon: '/images/games/snake.png',
        description: '控制蛇吃食物并不断变长，不要撞到墙壁或自己的身体！',
        bgColor: '#a2d149',
        playCount: 7654,
        isHot: true,
        path: '/pages/games/snake/snake'
      },
      {
        id: 3,
        name: '记忆翻牌',
        icon: '/images/games/memory.png',
        description: '考验记忆力的翻牌游戏，找出所有匹配的卡片对。',
        bgColor: '#6a89cc',
        playCount: 5432,
        isNew: true,
        path: '/pages/games/memory/memory'
      },
      {
        id: 4,
        name: '俄罗斯方块',
        icon: '/images/games/tetris.png',
        description: '经典的俄罗斯方块游戏，操控不同形状的方块，消除完整的行。',
        bgColor: '#7f8c8d',
        playCount: 8765,
        isHot: true,
        path: '/pages/games/tetris/tetris'
      },
      {
        id: 5,
        name: '数独挑战',
        icon: '/images/games/sudoku.png',
        description: '填写数字1-9，使每行、每列和每个九宫格内数字不重复。',
        bgColor: '#3498db',
        playCount: 4321,
        isNew: true,
        path: '/pages/games/sudoku/sudoku'
      },
      {
        id: 6,
        name: '飞行射击',
        icon: '/images/games/shooter.png',
        description: '控制飞机躲避障碍物并射击敌机，看看你能坚持多久！',
        bgColor: '#273c75',
        playCount: 6543,
        path: '/pages/games/shooter/shooter'
      },
      {
        id: 7,
        name: '连连看',
        icon: '/images/games/lianliankan.png',
        description: '经典的连连看游戏，在规定时间内消除所有相同的图案。',
        bgColor: '#e67e22',
        playCount: 7890,
        isHot: true,
        path: '/pages/games/lianliankan/lianliankan'
      },
      {
        id: 8,
        name: '猜谜语',
        icon: '/images/games/riddles.png',
        description: '各种有趣的谜语等你来猜，锻炼思维能力。',
        bgColor: '#9b59b6',
        playCount: 3456,
        path: '/pages/games/riddles/riddles'
      }
    ],
    recentGames: []
  },

  onLoad: function() {
    this.generateRandomImages();
    // 获取最近玩过的游戏记录
    const recentGames = wx.getStorageSync('recentGames') || [];
    this.setData({ recentGames });
  },
  
  onShow: function() {
    // 每次显示页面时更新最近游戏记录
    const recentGames = wx.getStorageSync('recentGames') || [];
    this.setData({ recentGames });
  },

  // 生成随机图片
  generateRandomImages: function() {
    const randomImageUrls = {
      game2048: 'https://picsum.photos/200/200?random=1',
      snake: 'https://picsum.photos/200/200?random=2',
      memory: 'https://picsum.photos/200/200?random=3',
      tetris: 'https://picsum.photos/200/200?random=4',
      sudoku: 'https://picsum.photos/200/200?random=5',
      shooter: 'https://picsum.photos/200/200?random=6',
      lianliankan: 'https://picsum.photos/200/200?random=7',
      riddles: 'https://picsum.photos/200/200?random=8'
    };

    // 更新游戏列表的图标
    const gameList = this.data.gameList.map((game, index) => ({
      ...game,
      icon: randomImageUrls[Object.keys(randomImageUrls)[index]]
    }));

    this.setData({
      randomImages: randomImageUrls,
      gameList: gameList
    });

    // 更新最近游戏的图标
    const recentGames = this.data.recentGames.map(game => {
      const originalGame = gameList.find(g => g.id === game.id);
      return {
        ...game,
        icon: originalGame ? originalGame.icon : game.icon
      };
    });

    this.setData({ recentGames });
  },

  // 打开游戏
  openGame: function(e) {
    const gameId = e.currentTarget.dataset.id;
    const game = this.data.gameList.find(item => item.id === gameId);
    
    if (game) {
      // 记录最近玩过的游戏
      this.addRecentGame(game);
      
      // 如果有实际路径，则导航到游戏页面
      if (game.path) {
        wx.navigateTo({
          url: game.path
        });
      } else {
        // 游戏尚未实现
        wx.showToast({
          title: '游戏开发中，敬请期待！',
          icon: 'none'
        });
      }
    }
  },
  
  // 添加到最近玩过的游戏
  addRecentGame: function(game) {
    let recentGames = wx.getStorageSync('recentGames') || [];
    
    // 移除已有的相同游戏(如果存在)
    recentGames = recentGames.filter(item => item.id !== game.id);
    
    // 将新游戏添加到最前面
    recentGames.unshift({
      id: game.id,
      name: game.name,
      icon: game.icon,
      lastPlayTime: new Date().getTime()
    });
    
    // 保留最近的5个游戏
    if (recentGames.length > 5) {
      recentGames = recentGames.slice(0, 5);
    }
    
    // 保存到本地存储
    wx.setStorageSync('recentGames', recentGames);
    this.setData({ recentGames });
  },
  
  // 格式化游戏时间
  formatLastPlayTime: function(timestamp) {
    const now = new Date();
    const lastPlay = new Date(timestamp);
    const diff = (now - lastPlay) / 1000; // 秒数差
    
    if (diff < 60) {
      return '刚刚';
    } else if (diff < 3600) {
      return Math.floor(diff / 60) + '分钟前';
    } else if (diff < 86400) {
      return Math.floor(diff / 3600) + '小时前';
    } else {
      return Math.floor(diff / 86400) + '天前';
    }
  },
  
  // 格式化游玩次数
  formatPlayCount: function(count) {
    if (count < 1000) {
      return count.toString();
    } else if (count < 10000) {
      return (count / 1000).toFixed(1) + 'K';
    } else {
      return (count / 10000).toFixed(1) + '万';
    }
  }
})
